/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "parametrii.h"
#include "database.h"

using namespace SQL;

Parametrii::Parametrii(QWidget* parent, Qt::WFlags fl)
		: KontAMabilWindow(parent, fl), Ui::parametriiForm(),m_inadaugare(false)
{
	setupUi(this);
	connect(tableview,SIGNAL(escapePressed()), defaultDatabase()->query("parametrii_masini"), SLOT(close()));
	connect(modeleLookup,SIGNAL(returnPressed()),this,SLOT(showParametrii()));
	connect(tableview,SIGNAL(escapePressed()),this,SLOT(hideParametrii()));
}

bool Parametrii::addParametru(const QString & addStr, QString * result, qint64 * id)
{
	if (!defaultDatabase()->query("parametrii_masini")->canInsert())
		return false;
	m_inadaugare=true;
	showParametrii();
	defaultDatabase()->query("parametrii_masini")->insert();
	denumireEdit->setText(addStr);
	denumireEdit->setCursorPosition(addStr.length());
	denumireEdit->setFocus();

	connect(defaultDatabase()->query("parametrii_masini"),SIGNAL(afterPost()),SLOT(accept()));
	connect(defaultDatabase()->query("parametrii_masini"),SIGNAL(afterCancel()),SLOT(reject()));
	bool res=exec();
	m_inadaugare=false;
	hideParametrii();
	disconnect(defaultDatabase()->query("parametrii_masini"),SIGNAL(afterPost()),this, SLOT(accept()));
	disconnect(defaultDatabase()->query("parametrii_masini"),SIGNAL(afterCancel()),this, SLOT(reject()));
	if (res)
	{
		if (result)
			*result=defaultDatabase()->query("parametrii_masini")->value("denumire").toString();
		if (id)
			*id=defaultDatabase()->query("parametrii_masini")->value("id").toInt();
	}
	return res;
}

void Parametrii::showEvent ( QShowEvent * event )
{
	if (!m_inadaugare)
		hideParametrii();
	KontAMabilWindow::showEvent(event);
}

void Parametrii::showParametrii()
{
	defaultDatabase()->query("parametrii_masini")->bindValue( "id_model", defaultDatabase()->query("modeleMasini")->value( "id" ) );
	defaultDatabase()->query("parametrii_masini")->select();
	parametriiFrame->setVisible( true );
	tableview->setFocus();
	modeleLookup->setEnabled(false);
}
void Parametrii::hideParametrii()
{
	parametriiFrame->setVisible( false );
	modeleLookup->setEnabled(true);
	modeleLookup->setFocus();
	layout()->activate();
	resize(QSize(width(), height() - parametriiFrame->height()));
}

Parametrii::~Parametrii()
{
}

